package com.itdianzi.controller.cargo;

import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.cargo.Contract;
import com.itdianzi.domain.cargo.ContractExample;
import com.itdianzi.service.cargo.ContractService;

import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer size) {
        //需要根据企业ID分类
        ContractExample example = new ContractExample();
        //根据日期排序   order by 字段 asc/desc
        example.setOrderByClause("create_Time desc");
        //封装条件
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(super.companyId);

        /**
         * 0-saas管理员
         * 1-企业管理员
         * 2-管理所有下属部门和人员
         * 3-管理本部门
         * 4-普通员工
         * 细粒度权限的条件
         * 普通员工 degree = 4
         */
        if (loginUser.getDegree() == 4) {//普通员工只能看自己的数据
            System.out.println(4);
            criteria.andCreateByEqualTo(loginUser.getId());
        } else if (loginUser.getDegree() == 3) {//部门经理查看本部门下的所有数据
            System.out.println(3);
            criteria.andCreateDeptEqualTo(loginUser.getDeptId());
        } else if (loginUser.getDegree() == 2) {//区域经理，看到整个区域下所有的数据
            System.out.println(2);
            criteria.andCreateDeptLike(loginUser.getDeptId() + "%");
        }



        PageInfo pageInfo = contractService.findAll(example, page, size);
        request.setAttribute("page",pageInfo);
        return "cargo/contract/contract-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {

        return "cargo/contract/contract-add";
    }
    @RequestMapping("/edit")
    public String edit(Contract contract) {
        /*System.out.println(contract);*/
        contract.setCompanyId(super.companyId);
        contract.setCompanyName(super.companyName);

        if (StringUtils.isBlank(contract.getId())) {
            System.out.println(1);

            contract.setCreateBy(loginUser.getId());//创建得人
            contract.setCreateDept(loginUser.getDeptId());//创建的部门

            contractService.save(contract);


        } else {
            System.out.println(2);

            contract.setUpdateBy(loginUser.getId());
            contract.setCreateTime(new Date());

            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "cargo/contract/contract-update";
    }
    @RequestMapping("/toView")
    public String toView(String id) {
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "cargo/contract/contract-view";
    }

    @RequestMapping("/submit")
    public String submit(String id) {

        //1.根据id查询到对象  修改id 再保存到数据库
        //2.利用mybatis生成的update方法
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(1);//提交状态
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping("/cancel")
    public String cancel(String id) {
        //1.根据id查询到对象  修改id 再保存到数据库
        //2.利用mybatis生成的update方法
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);//提交状态
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }
}
